package com.bbtShop.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bbtShop.config.BizException;
import com.bbtShop.core.model.Ids;
import com.bbtShop.core.model.ProductCategory;
import com.bbtShop.core.model.ProductRecode;
import com.bbtShop.core.model.ResultCode;
import com.bbtShop.dao.ProductCategoryMapper;
import com.bbtShop.dao.ProductRecodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author : manke
 * @Description
 * @Date: 2020/10/18 19:06
 */
@Service
public class ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    public IPage<ProductCategory> getLisProductCategory(Integer pageNum, Integer pageSize,
                                                      ProductCategory productCategory) {
        IPage<ProductCategory> page = new Page<>(pageNum == null ? 1 : pageNum,
                pageSize == null ? 10 : pageSize);
        LambdaQueryWrapper<ProductCategory> categoryLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (productCategory.getCategoryName() !=null&&!productCategory.getCategoryName().isEmpty()){
            categoryLambdaQueryWrapper.like(ProductCategory::getCategoryName,productCategory.getCategoryName());
        }
        return productCategoryMapper.selectPage(page,categoryLambdaQueryWrapper );


    }

    public void addUpdateProductCategory(ProductCategory productCategory) {
        if (productCategory.getId() == null) {
            productCategory.setAddTime(new Date());
            productCategory.setUpdTime(new Date());
            productCategoryMapper.insert(productCategory);
        } else {
            productCategory.setUpdTime(new Date());
            productCategoryMapper.updateById(productCategory);
        }

    }

    public void delProductCategory(Ids ids) {
        productCategoryMapper.deleteBatchIds(ids.getIds());

    }

    public ProductCategory getProductRecodeInfo(Integer id) {
        return productCategoryMapper.selectById(id);

    }
}
